package cn.wjh.business.service.impl;

import cn.wjh.business.domain.AuthRecord;
import cn.wjh.business.mapper.AuthRecordMapper;
import cn.wjh.business.service.IAuthRecordService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.service.ISysPostService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 认证记录Service业务层处理
 *
 * @author jiraWu
 * @date 2023-10-27
 */
@Service
public class AuthRecordServiceImpl extends ServiceImpl<AuthRecordMapper, AuthRecord> implements IAuthRecordService {
	@Resource
	private AuthRecordMapper authRecordMapper;
	@Resource
	private ISysPostService postService;

	@Resource
	private ISysUserService userService;

	/**
	 * 查询认证记录
	 *
	 * @param authRecordId 认证记录主键
	 * @return 认证记录
	 */
	@Override
	public AuthRecord selectAuthRecordByAuthRecordId(Long authRecordId) {
		return authRecordMapper.selectAuthRecordByAuthRecordId(authRecordId);
	}

	/**
	 * 查询认证记录列表
	 *
	 * @param authRecord 认证记录
	 * @return 认证记录
	 */
	@Override
	public List<AuthRecord> selectAuthRecordList(AuthRecord authRecord) {
		return authRecordMapper.selectAuthRecordList(authRecord);
	}

	/**
	 * 新增认证记录
	 *
	 * @param authRecord 认证记录
	 * @return 结果
	 */
	@Override
	public int insertAuthRecord(AuthRecord authRecord) {
		authRecord.setStatus(null);
		authRecord.setAuthRecordId(null);
		authRecord.setReason(null);
		Long userId = SecurityUtils.getUserId();
		LambdaQueryWrapper<AuthRecord> selectWrapper = new QueryWrapper<AuthRecord>().lambda()
				.eq(AuthRecord::getUserId, userId)
				.eq(AuthRecord::getStatus, 0);
		SysPost sysPost = postService.selectPostById(authRecord.getPostId());
		if (StringUtils.isNull(sysPost))
			throw new ServiceException("您申请的身份不在系统身份内,无法提交认证", 500);
		long count = this.count(selectWrapper);
		if (count > 0)
			throw new ServiceException("您还有身份认证记录在审核中,无法提交认证", 400);
		List<Long> postIds = SecurityUtils.getPostIds();
		if (postIds.contains(authRecord.getPostId()))
			throw new ServiceException("您不可以申请已经拥有的身份", 400);
		authRecord.setCreateTime(DateUtils.getNowDate());
		authRecord.setCreateBy(SecurityUtils.getUsername());
		authRecord.setDeptId(SecurityUtils.getDeptId());
		authRecord.setUserId(userId);
		return authRecordMapper.insertAuthRecord(authRecord);
	}

	/**
	 * 修改认证记录
	 *
	 * @param authRecord 认证记录
	 * @return 结果
	 */
	@Override
	public int updateAuthRecord(AuthRecord authRecord) {
		authRecord.setUpdateTime(DateUtils.getNowDate());
		return authRecordMapper.updateAuthRecord(authRecord);
	}

	/**
	 * 批量删除认证记录
	 *
	 * @param authRecordIds 需要删除的认证记录主键
	 * @return 结果
	 */
	@Override
	public int deleteAuthRecordByAuthRecordIds(Long[] authRecordIds) {
		return authRecordMapper.deleteAuthRecordByAuthRecordIds(authRecordIds);
	}

	/**
	 * 删除认证记录信息
	 *
	 * @param authRecordId 认证记录主键
	 * @return 结果
	 */
	@Override
	public int deleteAuthRecordByAuthRecordId(Long authRecordId) {
		return authRecordMapper.deleteAuthRecordByAuthRecordId(authRecordId);
	}

	/**
	 * 根据用户Id和审核状态查询认证资料
	 *
	 * @return
	 */
	@Override
	public AuthRecord getInfoByUserIdAndStatus() {
		LambdaQueryWrapper<AuthRecord> selectWrapper = new QueryWrapper<AuthRecord>().lambda()
				.eq(AuthRecord::getUserId, SecurityUtils.getUserId()).orderByDesc(AuthRecord::getCreateTime)
				.last("limit 1");
		return super.getOne(selectWrapper);
	}

	/*审核认证资料*/
	@Override
	@Transactional
	public boolean review(AuthRecord authRecord) {
		AuthRecord authRecord1 = new AuthRecord();
		authRecord1.setStatus(authRecord.getStatus());
		authRecord1.setAuthRecordId(authRecord.getAuthRecordId());
		authRecord1.setReason(authRecord.getReason());
		/*更新认证状态*/
		LambdaQueryWrapper<AuthRecord> updateWrapper = new QueryWrapper<AuthRecord>().lambda()
				.eq(AuthRecord::getStatus, 0)//只有待审核状态才能审核
				.eq(AuthRecord::getAuthRecordId, authRecord.getAuthRecordId());//记录Id
		boolean updated = super.update(authRecord1, updateWrapper);
		/*认证记录*/
		AuthRecord record = authRecordMapper.selectAuthRecordByAuthRecordId(authRecord.getAuthRecordId());
		if (StringUtils.isNull(record)) throw new ServiceException("记录不存在!");
		/*只有审核通过才能更新用户*/
		if (authRecord.getStatus().equals(1)) {
			/*更新用户身份*/
			SysUser user = new SysUser();
			user.setUserId(record.getUserId());//设置用户Id
			Long postId = record.getPostId();//身份
			if (postId.equals(1L)) {
				/*管理员认证*/
				user.setRoleIds(Collections.singletonList(100L));
			}
			user.setPostIds(Collections.singletonList(postId));//设置身份
			user.setUpdateTime(DateUtils.getNowDate());
			user.setUpdateBy(SecurityUtils.getUsername());
			userService.updateUser(user);//更新用户
		}
		return updated;
	}
}
